package com.peek.controller;

import com.peek.util.HelpMe;
import com.xiaoleilu.hutool.system.OsInfo;
import com.xiaoleilu.hutool.system.SystemUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

@Controller
@RequestMapping("/system")
public class SystemController {

	private static Logger log = LoggerFactory.getLogger(SystemController.class);
	private static OsInfo osInfo = SystemUtil.getOsInfo();

	@RequestMapping("/")
	public String index(HttpServletRequest request,Model model){



		return "system";
	}



	@RequestMapping("/results")
	@ResponseBody
	public String getResults(HttpServletRequest request,String command) throws Exception {

		command = command.trim();

		if (HelpMe.isNotNull(command)){
			String[] cmdA = { "/bin/sh", "-c", command};
			Process process = Runtime.getRuntime().exec(cmdA);
			LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line).append(System.lineSeparator());
			}
			log.info("命令执行结果 --> "+sb.toString());
			return sb.toString();
		}
		return null;
	}











}



















